# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from jsonfield import JSONField
from user.models import User
from me.exceptions import MeException
from me.db_utils import ShortUUIDField


class Album(models.Model):
    id = ShortUUIDField(primary_key=True, max_length=10, auto_created=True)
    user = models.ForeignKey(User, verbose_name=u'作者', on_delete=True)
    title = models.CharField(u'标题', max_length=40, null=True, blank=True)
    bg_music = JSONField(u'背景音乐', default={}, null=True, blank=True)
    cover = JSONField(u'封面图', default={}, null=True, blank=True)
    datas = JSONField(u'数据', default=[], null=True, blank=True)
    is_publish = models.BooleanField(u'公开', default=False)
    create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
    update_time = models.DateTimeField(u'更新时间', auto_now=True)

    class Meta:
        verbose_name = u'相册'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s:%s' % (self.id, self.title)


def is_valid_image(data):
    """
    图片格式
    {
        "type": "image",  // 数据项类型
        "url": "http://xxxx",
        "source": "qcloud",
        "width": 100,
        "height": 100,
        "se": { // 特效
            "animation": "zoomIn",  // 动画效果
            "duration": "2000"      // 展示时间
        }
    }
    """
    try:
        assert isinstance(data, dict)
        assert set(["type", 'url', 'source', 'width', 'height', 'se', ]).issubset(data.keys())
        assert isinstance(data.get('se'), dict)
        assert data.get('type') in ['image', ]
    except:
        raise MeException(2001)


# 素材类型
RESOURCES = (
    ('image', is_valid_image),
)

RESOURCE_TYPES = [i[0] for i in RESOURCES]
RESOURCE_VALIDATORS = {i[0]: i[1]for i in RESOURCES}
